﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Web.Configuration;
using System.Net;
using WXYW.DAL;
using WXYW.Model;
using System.Configuration;

namespace WXYW.BLL
{
    public static class EmailService
    {
        public static void SendEmail(string toAddress, string subject, string body)
        {
            try
            {
                if (!string.IsNullOrEmpty(WebConfigurationManager.AppSettings["MailServerHost"]))
                {
                    SmtpClient emailClient = new SmtpClient(WebConfigurationManager.AppSettings["MailServerHost"], 25);
                    if (!string.IsNullOrEmpty(WebConfigurationManager.AppSettings["MailServerUserName"]))
                    {
                        emailClient.Credentials = new NetworkCredential(WebConfigurationManager.AppSettings["MailServerUserName"],
                            WebConfigurationManager.AppSettings["MailServerPassword"]);
                    }
                    emailClient.EnableSsl = false;

                    MailMessage mailMessage = new MailMessage(WebConfigurationManager.AppSettings["MailServerUserName"], toAddress);
                    mailMessage.Subject = subject;
                    mailMessage.Body = body;
                    mailMessage.IsBodyHtml = true;
                    emailClient.Send(mailMessage);
                }
                else
                {
                    SmtpClient emailClient = new SmtpClient(ConfigurationManager.AppSettings["MailServerHost"], 25);
                    if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["MailServerUserName"]))
                    {
                        emailClient.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["MailServerUserName"],
                            ConfigurationManager.AppSettings["MailServerPassword"]);
                    }
                    emailClient.EnableSsl = false;

                    MailMessage mailMessage = new MailMessage(ConfigurationManager.AppSettings["MailServerUserName"], toAddress);
                    mailMessage.Subject = subject;
                    mailMessage.Body = body;
                    mailMessage.IsBodyHtml = true;
                    emailClient.Send(mailMessage);
                }
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, string.Format("SendEmail::{0}::{1}::{2}", toAddress, subject, body));
            }
        }

        /// <summary>
        /// 发邮件给派单员
        /// "{0}, 您好: {1}区域客户于{2}提交了{3}送修申请，请您在2个工作小时内分配单号，谢谢！"
        /// </summary>
        /// <param name="customer"></param>
        public static void SendEmailToPDYWhenSubitApplicationForm(User customer, string applicationType)
        {
            RoleService roleService = new RoleService();
            List<User> userList = roleService.GetUserHasPermission(3);
            string body = WebConfigurationManager.AppSettings["SubmitApplication"];

            foreach (User serviceuser in userList)
            {
                EmailService.SendEmail(serviceuser.UserEmail, "RMA单号分配通知", string.Format(body, serviceuser.UserName,
                    customer.UserAddress, DateTime.Now.ToString("yyyy年MM月dd日 dddd", new System.Globalization.CultureInfo("zh-CN")), applicationType));
            }
        }

        public static void SendEmailToBJY(string rmaNo, string userName)
        {
            RoleService roleService = new RoleService();
            List<User> userList = roleService.GetUserHasPermission(8);
            string body = WebConfigurationManager.AppSettings["NeedConfirmQuatation"];

            foreach (User serviceuser in userList)
            {
                EmailService.SendEmail(serviceuser.UserEmail, "报价确认通知", string.Format(body, rmaNo, userName));
            }
        }

        public static void SendEmailToSKY(string rmsNo, string userName)
        {
            RoleService roleService = new RoleService();
            List<User> userList = roleService.GetUserHasPermission(8);
            string body = WebConfigurationManager.AppSettings["NeedGathering"];

            foreach (User serviceuser in userList)
            {
                EmailService.SendEmail(serviceuser.UserEmail, "收款确认通知", string.Format(body, rmsNo, userName));
            }
        }
    }
}
